package com.ks.utils.dbBack;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * MySQL数据库备份
 * 
 * @author Kingsing zhueng
 *
 */
public class MySQLDatabaseBackup extends DatabaseBackup {

	private String binPath;
	private String hostIP;
	private String userName;
	private String password;
	private String savePath;
	private String fileName;
	private String databaseName;

	public MySQLDatabaseBackup() {
		super();
	}

	public MySQLDatabaseBackup(String binPath, String hostIP, String userName, String password, String savePath,
			String fileName, String databaseName) {
		this.binPath = binPath;
		this.hostIP = hostIP;
		this.userName = userName;
		this.password = password;
		this.savePath = savePath.replace("\" ", File.separator);
		this.fileName = fileName;
		this.databaseName = databaseName;
	}

	@Override
	public String packCommendExec() {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(binPath);
		stringBuilder.append(File.separator);
		stringBuilder.append("mysqldump").append(" --opt").append(" -h").append(hostIP);
		stringBuilder.append(" -u").append(userName).append(" -p").append(password).append(" --lock-all-tables=true");
		stringBuilder.append(" --default-character-set=utf8 ").append(databaseName);
		// stringBuilder.append(" --result-file=").append(savePath +
		// fileName).append(" --default-character-set=utf8
		// ").append(databaseName);
		return stringBuilder.toString();
	}

	@Override
	public boolean backup() {
		System.out.println("开始备份...");
		try {
			Runtime rt = Runtime.getRuntime();
			// 调用 调用mysql的安装目录的命令
			String commendExec = packCommendExec();
			Process child = rt.exec(commendExec);
			System.out.println(commendExec);

			// 设置导出编码为utf-8。这里必须是utf-8
			// 把进程执行中的控制台输出信息写入.sql文件，即生成了备份文件。注：如果不对控制台信息进行读出，则会导致进程堵塞无法运行
			InputStream in = child.getInputStream();// 控制台的输出信息作为输入流
			InputStreamReader xx = new InputStreamReader(in, "utf-8");
			// 设置输出流编码为utf-8。这里必须是utf-8，否则从流中读入的是乱码
			String inStr;
			StringBuffer sb = new StringBuffer("");
			String outStr;
			// 组合控制台输出信息字符串
			BufferedReader br = new BufferedReader(xx);
			while ((inStr = br.readLine()) != null) {
				// System.out.println(inStr);
				sb.append(inStr + "\r\n");
			}
			outStr = sb.toString();
			// 要用来做导入用的sql目标文件：
			FileOutputStream fout = new FileOutputStream(packPath(savePath, fileName));
			OutputStreamWriter writer = new OutputStreamWriter(fout, "utf-8");
			writer.write(outStr);
			writer.flush();
			in.close();
			xx.close();
			br.close();
			writer.close();
			fout.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public static void main(String[] args) {
		MySQLDatabaseBackup m = new MySQLDatabaseBackup("D://Program Files//MariaDB//bin//", "localhost", "root",
				"root", "D://backupDatabase//", "123123.sql", "tt");
		if (m.backup()) {
			System.out.println("备份成功...");
		} else {
			System.out.println("备份失败...");
		}
	}

}
